package com.lxp.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.eduService.domain.EduTeacher;
import com.lxp.eduService.domain.vo.queryVo.TeacherQuery;
import com.lxp.eduService.service.EduTeacherService;


import com.lxp.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lxp
 * @since 2021-11-09
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduService/edu-teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    //查找全部老师
    @ApiOperation("查找所有讲师")
    @GetMapping("/findAll")
    public R findAllTeacher(){

        //调用teachService的查询方法
        List<EduTeacher> eduTeachers = teacherService.list(null);
        return R.ok().data("items",eduTeachers);
    }

    @ApiOperation("根据id逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        }
        return R.error();

    }

    //路径传值
    @ApiOperation("分页查询老师")
    @GetMapping("/pageListTeacher/{current}/{limit}")
    public R pageListTeacher(
            @PathVariable long current,
            @PathVariable int limit){
        Page <EduTeacher> pageTeacher =new Page<>(current,limit);
        //将查询结果封装到pageTeacher去
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal(); //获取总记录数
        List teacher = pageTeacher.getRecords();//获取老师集合
        //封装到R类去
        return R.ok().data("total",total).data("list",teacher);
    }

    //路径传值
    @ApiOperation("分页条件查询老师")
    //必须用PostMapping 才有请求体 GetMapping没有请求体
    @PostMapping("/pageListTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(
            @PathVariable long current,
            @PathVariable int limit,
            @RequestBody(required = false) TeacherQuery teacherQuery){
           //将前端的请求体封装到teacherQuery类去 并允许为空
        Page <EduTeacher> pageTeacher =new Page<>(current,limit);
        //创建条件查询类
        QueryWrapper<EduTeacher> wrapper =new QueryWrapper<>();
        //进行条件查询 必须要导spring的包
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //根据修改时间排序
        wrapper.orderByDesc("gmt_modified");


        //执行方法获取到pageTeacher类
        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal(); //获取总记录数
        List teacher = pageTeacher.getRecords();//获取老师集合
        //封装到R类去
        return R.ok().data("total",total).data("list",teacher);
    }

    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        return flag?R.ok():R.error();
    }

    @ApiOperation("根据ID查询讲师")
    @GetMapping("/selectTeacherById/{id}")
    public R selectTeacherById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        if (StringUtils.isEmpty(teacher)){
            return R.error();//查找不到
        }
        return R.ok().data("teacher",teacher);//查找到了
    }


    @ApiOperation("根据ID修改讲师")
    @PostMapping("/updateTeacherById")
    public R updateTeacherById(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        return flag?R.ok():R.error();
    }
}

